<?php $this->placeholder('big-section-class')->set('main_section grid_12') ?>
<?php $this->JQuery()->javascriptCaptureStart()?>

$j('#tips').tipsy({gravity: 'w', html: true, fallback: "<p style='text-align:left'><?php echo $this->t('Your logo will be displayed in:<br>1. Your Account<br>2. Sub-Accounts<br>3. Poster Profile, Search Results &<br>Ad Detail pages if you are a<br>Premium Account owner')?></p>" });

$j(document).ready(function() {
	
	if ($j.browser.msie) {
		$j('.logo_uploader_ie').show();
		$j('#container_uploader1').hide();
		
	} else {
		$j('.logo_uploader_ie').hide();
		$j('#container_uploader1').show();
		
	}
		
	var uploader1;
    	setTimeout(function() {
            uploader1.init();
            uploader1.bind('FilesAdded', function(up, files) {
                uploader1.start();
            });
        },200);
    	var temp = $j('.validation img').attr('src');
    	$j('.validation img').remove();
    	$j('.validation').append('<img class="xxx" height="50" width="240" src="' + temp + '">');
    	
    	temp = $j('#newaccount-newsletter');
    	$j('#newaccount-newsletter').remove();
    	$j('.newsletter label').prepend(temp);
    	
    	temp = $j('.validation .errors');
    	$j('.validation .errors').remove();
    	$j('.validation').append(temp);
    	$j('.validation .errors').css('float', 'left');
    	
    	//$('#fieldset-bizinfo').append('<div id="container_uploader1"><div class="photo"><img src="" id="company_pic"></div><div class="text button"><button type="button" id="pickfiles1"><?php echo $this->t('Browse')?></button></div></div>');
	
		uploader1 = new plupload.Uploader({
			runtimes : 'html5,flash,html4',
			browse_button : 'pickfiles1',
			container : 'container_uploader1',
			max_file_size : '10mb',
			url : '/default/upload/company-pic',
			flash_swf_url : '/plupload/js/plupload.flash.swf',
			silverlight_xap_url : '/plupload/js/plupload.silverlight.xap',
			filters : [
				{title : "Image files", extensions : "jpeg,jpg,gif,png"},
				{title : "Zip files", extensions : "zip"}
			],
			resize : {width : 320, height : 240, quality : 90}
		});
	
		uploader1.bind('Error', function(up, err) {
			$j('#filelist').append("<div>Error: " + err.code +
				", Message: " + err.message +
				(err.file ? ", File: " + err.file.name : "") +
				"</div>"
			);
	
			up.refresh(); // Reposition Flash/Silverlight
		});
	
		uploader1.bind('FileUploaded', function(up, file, res) {
	        var response = $j.parseJSON(res.response);
			$j('#' + file.id + " b").html("100%");
	        //var img = $j('img').attr("src",response.url).html();
	        //$j('.logo_preview').empty();
	        //$j(document.createElement("img"))
	        //    .attr({ src: response.url, rel:response.ori, id:'profile_pic_to_be'})
	        //    .appendTo($j('.logo_preview'))
			
	        $j.ajax({
	            type: 'POST',
	            url: '<?php echo $this->geturl('confirm-business-pic', 'user')?>',
	            data: {url:response.ori, 'type':'company'},
	            success:function () {
	            	$j('#logo_cont').show();
					$j('#company_logo').attr('src',response.url);
	            }
	        });
		});
	
		// Client side form validation
		$j('#form_uploader').submit(function(e) {
			var uploader1 = $j('#uploader').pluploadQueue();
	
			// Validate number of uploaded files
			if (uploader1.total.uploaded == 0) {
				// Files in queue upload them first
				if (uploader1.files.length > 0) {
					// When all files are uploaded submit form
					uploader1.bind('UploadProgress', function() {
						if (uploader1.total.uploaded == uploader1.files.length)
							$j('form').submit();
					});
	
					uploader1.start();
				} else
					alert('<?php echo $this->t('You must at least upload one file.')?>');
	
				e.preventDefault();
			}
		});
		
		$j('.remove_logo').click(function() {
	        $message = '<?php echo $this->t('Do you want to remove your logo?')?>';
	        if (confirm($message)) {
	            $j.post(
	                '<?php echo $this->geturl('delete-logo','user')?>',
	                {},
	                function (data) {
	                        $j('#company_logo').attr('src', '');       
							$j('#logo_cont').hide();                                 
	                }
	            );
	        }
	        return false;
	    });
    	
    	$j('#container_uploader1').css('float', 'none');
    	$j('#container_uploader1').css('top', '-50px');
		
		
	//form validations
	$j('#company_form').submit(function(e) {
		if ($j('#company_name').attr('value') == ''){
			$j('#cn_error').show();
			goToAnchor('company_name_div');
			return false;
		}
		
		if ($j('#cn_error').is(':visible')){
			goToAnchor('company_name_div');
			return false;
		}
		
		if ($j('#business_category').attr('value') == ''){
			$j('#bc_error').show();
			goToAnchor('business_category_div');
			return false;
		}
		
		/* if ($j('#contact_phone').attr('value') == ''){
			$j('#ph_error').show();
			goToAnchor('contact_phone_div');
			return false;
		} */
		
		if ($j('#contact_phone').attr('value') != ''){
			if ($j('#contact_phone').attr('value').match(/^\d+$/) == null){
				$j('#ph2_error').show();
				goToAnchor('contact_phone_div');
				return false;
			}
		}
		
		if ($j('#contact_email').attr('value') != '') {
			if ($j('#contact_email').attr('value').match(/^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/) == null) {
				$j('#pe_error').show(); 
				goToAnchor('contact_email_div');
				return false;
			}
		}
		
		if ($j('#website').attr('value') != '' && $j('#website').attr('value') != 'http://') {
			
				if ($j('#website').attr('value').match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) == null) {
					$j('#website_error').show(); 
					goToAnchor('website_div');
					return false;
				}
			
		}
		
		if ($j('#facebook_twitter').attr('value') != '' && $j('#facebook_twitter').attr('value') != 'http://') {
			if ($j('#facebook_twitter').attr('value').match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) == null) {
				$j('#fbtwitt_error').show(); 
				goToAnchor('facebook_twitter_div');
				return false;
			}
		}
		
        if ($j('#number_employees').attr('value')!='' && $j('#number_employees').attr('value').match(/^\d+$/) == null){
			$j('#ne_error').show();
			goToAnchor('number_employees_div');
			return false;
		}
                
		if ($j('#fax').attr('value')!='' && $j('#fax').attr('value').match(/^\d+$/) == null){
			$j('#fa_error').show();
			goToAnchor('fax_div');
			return false;		
		}
         
		if ($j('#phone').attr('value')!='' && $j('#phone').attr('value').match(/^\d+$/) == null){
			$j('#ph3_error').show();
			goToAnchor('phone_div');
			return false;		
		} 
		
	});
	
	//close_thankyou script
	$j('.close').click(function(){
		$j('.alert_success').remove();
	});
	
	$j('#company_name').blur(function() {
		if ($j('#company_name').attr('value') != ''){
			var com_name = '<?php echo $this->loggedin_user->company->company_name ?>';
			
			if (com_name!=$j('#company_name').attr('value')) {
				$j.post(
					'/default/index/search-company',
					{data:$j('#company_name').attr('value')},
					function (data) {
						if(data.res == 'not') {
							$j('#cn_error').show();
							$j('#cn_error li').text($j('#company_name').attr('value')+' <?php echo $this->t('is already taken')?>');
							return false;
						} else {
							$j('#cn_error').hide();
						}
						
					}
				);
				return false;
			}
		}
	
	});
	
	$j('#contact_phone').blur(function() {
		if ($j('#contact_phone').attr('value') == ''){
			$j('#ph2_error').hide();
			$j('#ph_error').hide();
		} else {
			$j('#ph_error').hide();
			if ($j('#contact_phone').attr('value').match(/^\d+$/) == null){
				$j('#ph2_error').show();
			} else {
				$j('#ph2_error').hide();
			}
		}
	});	
		
	$j('#website').blur(function() {
		if ($j('#website').attr('value') != '' && $j('#website').attr('value') != 'http://') {
			if ($j('#website').attr('value').match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) == null) {
				$j('#website_error').show(); 
			} else {
				$j('#website_error').hide(); 	
			}
		} else {
			$j('#website_error').hide(); 	
		}
	});
	
	$j('#facebook_twitter').blur(function() {
		if ($j('#facebook_twitter').attr('value') != '' && $j('#facebook_twitter').attr('value') != 'http://') {
			if ($j('#facebook_twitter').attr('value').match(/^(http|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) == null) {
				$j('#fbtwitt_error').show(); 
			} else {
				$j('#fbtwitt_error').hide(); 
			}
		} else {
			$j('#fbtwitt_error').hide(); 
		}
	});
	
	$j('#contact_email').blur(function() {
		if ($j('#contact_email').attr('value') != '') {
			if ($j('#contact_email').attr('value').match(/^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/) == null) {
				$j('#pe_error').show(); 
			} else {
				$j('#pe_error').hide(); 
			}
		} else {
			$j('#pe_error').hide(); 
		}	
	});
	
	$j('#phone').blur(function() {
		if ($j('#phone').attr('value')!='' && $j('#phone').attr('value').match(/^\d+$/) == null){
			$j('#ph3_error').show();
		} else {
			$j('#ph3_error').hide();
		}	
	});
	
	$j('#number_employees').blur(function() {
		if ($j('#number_employees').attr('value')!='' && $j('#number_employees').attr('value').match(/^\d+$/) == null){
			$j('#ne_error').show();
		} else {
			$j('#ne_error').hide();
		}
	});
	
	$j('#fax').blur(function() {
		if ($j('#fax').attr('value')!='' && $j('#fax').attr('value').match(/^\d+$/) == null){
			$j('#fa_error').show();
		} else {
			$j('#fa_error').hide();
		}
	});
	$j('#mailing_address').blur(function() {
		if ($j(this).attr('value') == '') {	
			$j(this).val('e.g.: 8 Lucky Building, 88 Caine Road, Central');
		}
	});
	$j('#mailing_address').focus(function() {
		if ($j(this).attr('value') == 'e.g.: 8 Lucky Building, 88 Caine Road, Central') {	
			$j(this).val('');
		}
	});
	
	function goToAnchor(id){
     	$j('html,body').animate({scrollTop: $j("#"+id).offset().top},'fast');
	}

});
<?php $this->JQuery()->javascriptCaptureEnd()?>
<?php $this->JQuery()->addJavascriptFile('http://bp.yahooapis.com/2.4.21/browserplus-min.js')?>

<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.v-'.AC_VERSION.'.js'))?>
<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html5.v-'.AC_VERSION.'.js'))?>
<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/plupload.html4.v-'.AC_VERSION.'.js'))?>

<?php $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/plupload/jquery.plupload.queue.min.v-'.AC_VERSION.'.js'))?>
<?php if (IS_LIVE):?>
	<?php $this->headLink()->appendStylesheet(Ac_Util::getImageBaseUrl('/css/plupload/plupload.queue.v-'.AC_VERSION.'.css'))?>
<?php endif;?>
<?php $this->jQuery()->uiEnable()?>
<?php echo $this->partial('index/my-account-side.phtml', array('select'=> 'company')) ?>
<?php $this->placeholder('container-class')->set('my_acc profile company')?>
<section>
	<?php if ($this->update_status == 1): ?>
		<p class="alert success alert_success"><?php echo $this->t('Your Information has been updated')?><a class="close" href="#">Close</a></p>
	<?php endif ?>
	<?php if(!empty($_GET['upgradesuccess']) && empty($this->update_status)):?>
		<p class="alert success alert_success"><?php echo $this->t('Account has been successfully upgraded')?><a class="close" href="#">Close</a></p>
	<?php endif ?>
	<section class="business_directory grid_8">
		<form id="company_form" method="post" enctype="multipart/form-data">
			<fieldset>
				<legend><?php echo $this->t('Business Information')?></legend>
				<div class="required desc"><?php echo $this->t('Required Information')?></div>
				<div id="company_name_div" class="text company_name col_6 item_1">
					<label class="required"><?php echo $this->t('Company Name')?></label>
					<span><?php echo $this->t('4-30 Characters')?></span>
					<input type="text" id="company_name" name="company_name" value="<?php echo $this->loggedin_user->company->company_name?>">
					<ul class="errors" style="display:none;" id="cn_error"><li><?php echo $this->t('This information is required')?></li></ul>
				</div>
				<div id="business_category_div" class="select business_directory col_6">
					<label class="required"><?php echo $this->t('Your Business Category')?></label>
					<span></span>
					<?php
                        $bd = Model_Category::one(array('id' => 'mc52'));
                        $id = $bd->_id;

                        $locs = Model_Category::all(array('_id' => array('$ne' => new MongoId($id)),  'path' => new MongoId($id), 'status' => Model_Category::STATUS_ACTIVE))->sort(array('pos' => 1));
                        ?>
						<select name="business_category" id="business_category">
							<option value=""><?php echo $this->t('Select')?></option>
							<?php foreach($locs as $k => $loc) { 
								if (count($loc->path) == 2) { ?>
									<option	value="<?php echo $loc->_id?>" <?php if ((string)$this->loggedin_user->company->business_category == $loc->_id) { echo "selected='selected'"; } ?>><?php echo $loc->name?></option>
							<?php	} 
							}?>
						</select>
						<ul class="errors" style="display:none;" id="bc_error"><li><?php echo $this->t('This information is required')?></li></ul>
				</div>
				<div id="contact_phone_div" class="text col_6">
					<label><?php echo $this->t('Company Phone')?></label>
					<span></span>
					<input type="text" name="contact_phone" id="contact_phone" value="<?php echo $this->loggedin_user->company->phone?>">
					<ul class="errors" style="display:none;" id="ph_error"><li><?php echo $this->t('This information is required')?></li></ul>
					<ul class="errors" style="display:none;" id="ph2_error"><li><?php echo $this->t('Numbers Only')?></li></ul>
				</div>
				<div id="fax_div" class="text col_6">
					<label><?php echo $this->t('Fax Number')?></label>
					<input type="text" name="fax" id="fax" value="<?php echo $this->loggedin_user->company->fax?>">
					<ul class="errors" style="display:none;" id="fa_error"><li><?php echo $this->t('Numbers Only')?></li></ul>
				</div>
				<div id="mailing_address_div" class="textarea mailing_address">
					<label><?php echo $this->t('Mailing Address')?></label>
					<span><?php echo $this->t('Please include both English and Chinese address')?></span>
					<script language="javascript">
						function clearText(thefield){
							if (thefield.defaultValue=='e.g.: 8 Lucky Building, 88 Caine Road, Central')
								thefield.value = '';
						}
					</script>
					<textarea name="mailing_address" id="mailing_address"><?php echo (!empty($this->loggedin_user->company->mailing_address)) ? $this->loggedin_user->company->mailing_address : 'e.g.: 8 Lucky Building, 88 Caine Road, Central'?></textarea>	
				</div>
				<div id="number_employees_div" class="text  col_6">
					<label><?php echo $this->t('No. of Employees')?></label>
					<input type="text" name="number_employees" id="number_employees" value="<?php echo $this->loggedin_user->company->number_employees?>">
					<ul class="errors" style="display:none;" id="ne_error"><li><?php echo $this->t('Numbers Only')?></li></ul>
				</div>

				<div class="select col_6">
					<label><?php echo $this->t('District')?></label>
					<?php
					$locs = Model_Location::all()->sort(array('path' => 1));
					$parent = '';
					$locations=array();
					foreach($locs as $k => $loc) {
						if (count($loc->path) == 1) {
							$parent = $loc->name;
							$locations[$loc->name] = array();
						} else {
							$tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();
							$tmp[$k]         = (string) $loc->name;
							$locations[(string) $parent] = $tmp;
						}
					} ?>

					<select name="location" id="location">
						<option value=""><?php echo $this->t('Select')?></option>
					<?php foreach($locations as $k => $locs):?>
						<optgroup label="<?php echo $k?>">
						<?php foreach($locs as $j => $loc):?>
							<option value="<?php echo $j?>" <?php if ($this->loggedin_user->company->lo == $j) { echo "selected='selected'"; } ?>><?php echo $loc?></option>
						<?php endforeach;?>
						</optgroup>
					<?php endforeach;?>
					</select>
					<ul class="errors" style="display:none;" id="lo_error"><li><?php echo $this->t('This information is required')?></li></ul>
				</div>
				<div id="website_div" class="text company_website">
					<label><?php echo $this->t('Company Website')?></label>
					<span></span>
					<input type="text" name="website" id="website" value="<?php echo (!empty($this->loggedin_user->company->website)) ? $this->loggedin_user->company->website : 'http://'?>">
					<ul class="errors" style="display:none;" id="website_error"><li><?php echo $this->t('Please enter a valid URL')?></li></ul>
				</div>
				<div id="facebook_twitter_div" class="text">
					<label><?php echo $this->t('Facebook / Twitter')?></label>
					<span></span>
					<input type="text" name="facebook_twitter" id="facebook_twitter" value="<?php echo (!empty($this->loggedin_user->company->facebook_twitter)) ? $this->loggedin_user->company->facebook_twitter : 'http://'?>">
					<ul class="errors" style="display:none;" id="fbtwitt_error"><li><?php echo $this->t('Please enter a valid URL')?></li></ul>
				</div>
		<!--		<div style="background:none !important;" class="file text button company_logo" id="container_uploader1">
					<label><?php echo $this->t('Company Logo')?></label>
					<span><?php echo $this->t('JPG, GIF or PNG format, size limit 2MB')?></span>
					<div class="photo">
						<img src="<?php echo $this->thumbnail()->scale($this->loggedin_user->company->image, 280, 280);?>" id="company_pic">
					</div>
					<input type="text"><button type="button" id="pickfiles1"><?php echo $this->t('Browse...')?></button>
				</div>
				<div class="desc">
				<?php echo $this->t('By uploading this image, you certify that you have the right to distribute it and that it does not violate our <a href="#">Terms of Use</a>.')?>
				</div>
		-->
				<div id="form_uploader_cnt">
			<div id="form_uploader" class="button">
		        <label><?php echo $this->t('Company Logo')?></label>
		        <span><?php echo $this->t('4MB max each. Format: JPEG, JPG, GIF, PNG')?></span>
		        <div id="container_uploader1" class="button uploader_cont">
		            <button id="pickfiles1" type="button"><?php echo $this->t('Browse ...')?></button>
		        </div>
				<div class="file logo_uploader_ie">
		            <input type="file" id="file_upload_logo" name="uploadlogo">
		        </div>
	    	</div>
	    	<div id="profile_pic_cnt" class="logo_preview">
			   	<div id="logo_cont" class="selected" <?php if (empty($this->loggedin_user->company->image)){echo 'style="display:none;"';}?>>
	                <figure>
	                    <img id="company_logo" src="<?php echo $this->thumbnail()->scale($this->loggedin_user->company->image, 280, 280, true, true);?>">
	                </figure>
	                <a href="#" class="remove_logo"><?php echo $this->t('Remove')?></a>
	            </div>
            </div>
            <p></p>
		</div>
		<div class="desc">
			<?php echo $this->t('By uploading this image, you certify that you have the right to distribute it and that it does not violate our <a href="__1__">Terms of Use</a>.', null, '/default/index/term-of-use') ?>
    		
	    </div>

			</fieldset>
			<fieldset>
				<legend><?php echo $this->t('Business Contact Information')?></legend>
				<div class="desc"><?php echo $this->t("Provide your business's contact information so customers can contact you.")?></div>
					<div class="col_6 text">
						<label><?php echo $this->t('First Name')?></label>
						<input type="text" value="<?php echo $this->loggedin_user->company->contact_firstname?>" id="contact_firstname" name="contact_firstname">
					</div>
					<div class="col_6 text">
						<label><?php echo $this->t('Last Name')?></label>
						<input type="text" value="<?php echo $this->loggedin_user->company->contact_lastname?>" id="contact_lastname" name="contact_lastname">
					</div>
				<div class="text col_6 department">
					<label><?php echo $this->t('Department')?></label>
					<span></span>
					<input type="text" name="contact_department" id="contact_department" value="<?php echo $this->loggedin_user->company->contact_department?>">
				</div>
				<div class="text col_6 job_title">
					<label><?php echo $this->t('Job Title')?></label>
					<span></span>
					<input type="text" name="contact_job" id="contact_job" value="<?php echo $this->loggedin_user->company->contact_job?>">
				</div>
				<div id="phone_div" class="text mobile_phone">
					<label><?php echo $this->t('Mobile Phone')?></label>
					<span></span>
					<input type="text" name="phone" id="phone" value="<?php echo $this->loggedin_user->company->contact_phone?>">
					<ul class="errors" style="display:none;" id="ph3_error"><li><?php echo $this->t('Numbers Only')?></li></ul>
				</div>
				<div id="contact_email_div" class="text email_address">
					<label><?php echo $this->t('Email Address')?></label>
					<span>e.g. info@companyabc.com</span>
					<input type="text" name="contact_email" id="contact_email" value="<?php echo $this->loggedin_user->company->contact_email?>">
					<ul class="errors" style="display:none;" id="pe_error"><li><?php echo $this->t('Valid email required')?></li></ul>
				</div>
			</fieldset>
			<fieldset>
				<legend<?php echo $this->t('>About Us')?></legend>
				<div class="desc"><?php echo $this->t('Describe your business: Background, services, products and customer policies.')?></div>
				<div class="textarea about_us">
					<textarea name="about" id="about"><?php echo $this->loggedin_user->company->about?></textarea>
				</div>

			</fieldset>
			<fieldset>
				<legend><?php echo $this->t('Upload Photos')?></legend>
				<?php echo $this->Partial('/index/photo-uploader.phtml', 'user', array('company' => $this->loggedin_user->company));?>
				<div class="button em">
					<button><?php echo $this->t('Update')?></button>
				</div>
			</fieldset>
		
		</form>
	</section>
</section>
